<template>
  <div v-show="show" @click="top" class="index-container">top</div>
</template>

<script>
export default {
  data: () => ({
    show: false,
  }),
  created() {
    this.$bus.$on("mainScroll", this.scroll);
  },
  destroyed() {
    this.$bus.$emit("mainScroll", undefined); // 解决切换页面 组件还显示的问题
    this.$bus.$off("mainScroll", this.scroll);
  },
  methods: {
    scroll(dom) {
      if (!dom) {
        this.show = false;
        return;
      }
      this.show = dom.scrollTop >= 500;
    },
    top() {
      this.$bus.$emit("setMainScroll", 0);
    },
  },
};
</script>

<style lang='less' scoped>
@import "~@/assets/style/var.less";
.index-container {
  background: @primary;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  position: fixed;
  z-index: 99;
  bottom: 25px;
  right: 25px;
  cursor: pointer;
  color: #fff;
  text-align: center;
  line-height: 50px;
}
</style>
